package acwing;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class A851 {
    static int N=100050;
    static  int[] h=new int[N];
    static int[] e=new int[N];
    static int[] ne=new int[N];
    static int[] w=new int[N];//边权
    static int[] dist=new int[N];
    static boolean[] st=new boolean[N];
static int idx=1;
    static int n;
    static int m;

    static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
    static void add(int a,int b,int c){
        e[idx]=b;
        w[idx]=c;
        ne[idx]=h[a];
        h[a]=idx++;
    }
    public static void main(String[] args) throws IOException {

        String[] s = read.readLine().split(" ");
        n=Integer.parseInt(s[0]);
        m=Integer.parseInt(s[1]);

        int a,b,c;
        for(int i=1;i<=m;i++){
            String[] s1 = read.readLine().split(" ");
            a=Integer.parseInt(s1[0]);
            b=Integer.parseInt(s1[1]);
            c=Integer.parseInt(s1[2]);
            add(a,b,c);
        }
        spfa();
        int res=dist[n];
        if(res==Integer.MAX_VALUE) System.out.println("impossible");
        else System.out.println(res);

    }
static void spfa(){
    Arrays.fill(dist,Integer.MAX_VALUE);
    dist[1]=0;
        Queue<Integer> queue=new LinkedList<>();
        queue.offer(1);
        st[1]=true;
while(!queue.isEmpty()){
    //取出队伍头
    Integer poll = queue.poll();
    st[poll]=false;
    //更新最小值
    for(int edge=h[poll];edge!=0;edge=ne[edge]){
        int i = e[edge];
     //   dist[i]=Math.min(dist[i],dist[poll]+w[edge]);
        if(dist[i]>dist[poll]+w[edge]){
            dist[i]=dist[poll]+w[edge];
            //如果没在队列中
            if(!st[i]){
                queue.offer(i);
                st[i]=true;
            }
        }


    }
}
}
}
